1. Field of the Invention
The present invention relates to the field of programming languages and more specifically to compilation of programs having templates.
2. Description of the Related Art
The ANSI/ISO working group X3J16/WG21 has standardized a new programming language, C++, based upon C. One of the many features included in the language is xe2x80x9ctemplatesxe2x80x9d. A template allows the definition of a set of classes, functions, or variables, paramaterized by a set of template parameters. A program which references a template name must specify the values of the template parameters for the reference (either explicitly or implicitly). When a compiler encounters such a reference, the definition of the referenced template instance is generated from the template definition through a process known as instantiation.
One feature of most programming languages is the notion of separate compilation. This is illustrated by FIG. 1A. As shown in FIG. 1A, a plurality of separate source files 101, 102 and 103 are utilized as inputs to a compilation process. The source files include both source code and definitions of templates. Thus, separate compilation allows different parts of a complete program to be defined in multiple source files. Each source file 101-103 can be separately compiled into an object file 121-123 using a compilation step 111-113. Each source file thus compiled is referred to as a translation unit. A linker process 134 combines multiple object files into a single executable program 144.
FIG. 1B illustrates an alternative prior art process for forming an executable program. The source/definition 101 remains unchanged. However, the source for 102 and 103 includes a template instantiation in place of the definition. The source 101 is compiled first and provides as outputs an object file (as in FIG. 1A) and a template database 154. The template database, together with the source files 152 and 153 are used as inputs to the compilation process 112 and 113, respectively. In this way, the definition does not have to be repeated in each of source files 152 and 153.
Thus, while the process of FIG. 1B offers the advantage over FIG. 1A of avoiding repeating the template definitions, it requires that a separate template database 154 be provided. In addition, in both the process of FIG. 1A and FIG. 1B, a change in the template definition requires recompiling all of the source files.
The draft ANSI standard provides for the separate compilation of templates in C++. The definition of an instantiated template is only required to exist in one translation unit of the program. However, due to difficulties of implementing this strategy, there were proposals to the standard committee to relax this restriction.
This invention provides a simple method of implementing the separate compilation of templates by creating an executable program from source programs having templates comprising the steps of: providing a first source file having a template definition; providing a second source file having a template instantiation; compiling the first source file and the second source file into first and second object files, the first object file comprising a template definition; and linking the first and second object files into an executable program.